package com.clz.hyperit.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.clz.hyperit.dao.GoodsDao;
import com.clz.hyperit.dao.OrderDao;
import com.clz.hyperit.dao.RoleDao;
import com.clz.hyperit.dao.UserDetailDao;
import com.clz.hyperit.entity.Goods;
import com.clz.hyperit.entity.Order;
import com.clz.hyperit.entity.UserDetail;
import com.clz.hyperit.service.GoodsService;
import com.clz.hyperit.vo.GoodsOrder;

@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {
	private static Logger logger = LoggerFactory
			.getLogger(GoodsServiceImpl.class);

	@Autowired
	private GoodsDao goodsDao;

	@Autowired
	private UserDetailDao userDetailDao;

	@Autowired
	private OrderDao orderDao;

	public List<Goods> findGoods() {
		return goodsDao.findAll();
	}

	public Goods getByName(String name) {
		return goodsDao.getByName(name);
	}

	public Integer saveGoods(Goods goods) {
		return goodsDao.save(goods);
	}

	public void deleteGoods(Goods goods) {
		goodsDao.deleteById(goods.getId());
	}

	public void updateGoods(Goods goods) {
		goodsDao.updateWithNotNullProperties(goods);
	}

	public Goods getByID(Integer id) {
		return goodsDao.get(id);
	}

	public Order saveOrder(Goods goods, Integer userdetail_id) {
		Order order = new Order();
		UserDetail ud = userDetailDao.get(userdetail_id);
		goods = goodsDao.get(goods.getId());
		int money = ud.getHb() - goods.getCost();
		if (money <= 0 || goods.getStock() < 1||ud.getAddress()==null) {
			return null;
		} else {
			goods.setStock(goods.getStock() - 1);
			goodsDao.updateWithNotNullProperties(goods);
			ud.setHb(money);
			userDetailDao.updateWithNotNullProperties(ud);
			order.setGoods_id(goods.getId());
			order.setUserDetail_id(userdetail_id);
			order.setAddress(ud.getAddress());
			return orderDao.get(orderDao.save(order));
		}
	}

	public List<GoodsOrder> findPersonOrder(Integer userdetail_id) {
		Goods goods = null;
		GoodsOrder goodsorder = null;
		List<GoodsOrder> goodsorder_list = new ArrayList<GoodsOrder>();
		//UserDetail ud = userDetailDao.get(userdetail_id);
		List<Order> orderlist = orderDao.getByUserID(userdetail_id);
		for (Order order : orderlist) {
			goods = goodsDao.get(order.getGoods_id());
			goodsorder = new GoodsOrder(goods.getPath(), goods.getName(),
					goods.getCost(), order.getCreatetime(), order.getAddress());
			goodsorder_list.add(goodsorder);
		}
		return goodsorder_list;
	}

	public List<GoodsOrder> findOrder() {
		Goods goods = null;
		GoodsOrder goodsorder = null;
		//UserDetail ud = null;
		List<GoodsOrder> goodsorder_list = new ArrayList<GoodsOrder>();
		List<Order> orderlist = orderDao.findAll();
		for (Order order : orderlist) {
			goods = goodsDao.get(order.getGoods_id());
			//ud = userDetailDao.get(order.getUserDetail_id());
			goodsorder = new GoodsOrder(goods.getPath(), goods.getName(),
					goods.getCost(), order.getCreatetime(), order.getAddress());
			goodsorder_list.add(goodsorder);
		}
		return goodsorder_list;
	}
}
